import axios from 'axios';
import {BASE_URL, TIMEOUT} from "./config"


class HttpRequest {
    constructor(baseURL, timeout) {
        this.axiosInstance = axios.create({
            // 参数名很重要 要与create方法的参数名保持一致
            baseURL,
            timeout
        })

        this.axiosInstance.interceptors.response.use((res) => {
            return res.data
        }, err => {
            return err
        })
    }

    request(parameters) {
        return this.axiosInstance.request(parameters)
    }

    get(parameters) {
        return this.request({...parameters, method: "get"})
    }

    post(requestBody) {
        return this.request({...requestBody, method: "post"})
    }
}

const httpRequest = new HttpRequest(BASE_URL, TIMEOUT);
export default httpRequest;